<?php

namespace app\api\controller;

use addons\blockchain\model\VslBlockChainRecordsModel;
use addons\bonus\model\VslAgentLevelModel;
use data\model\UserModel;
use data\model\VslMemberModel;
use data\model\VslOrderGoodsExpressModel;
use data\model\VslOrderGoodsModel;
use data\model\VslOrderModel;
use data\service\Address as AddressService;
use data\service\Express;
use data\service\Express as ExpressService;
use data\service\Order\OrderGoods;
use data\service\Order\OrderStatus;
use data\service\Order as OrderService;
use data\service\Member as MemberService;
use data\service\Order\Order as OrderType;
use data\service\Excel;
use addons\distribution\model\VslOrderDistributorCommissionModel;
use addons\bonus\model\VslOrderBonusModel;
use data\model\VslGoodsModel;
use data\model\VslApiVisitLogModel;
/**
 * 订单控制器
 *
 * @author  www.vslai.com
 *
 */
class Order extends BaseController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 订单发货
     */
    public function orderDelivery()
    {
        if ($this->merchant_expire == 1) {
            return AjaxReturn(-1);
        }
        $order_service = new OrderService();
        $order_bn = request()->post('order_bn');
        $memo = request()->post('seller_memo');  
        $shipping_type = request()->post('shipping_type',0); 
        $logistics_arr = request()->post('logistics_arr');   
        if(empty($order_bn)){
            $this->update_logs(json_encode(['code'=>-2,'message'=>'缺少order_bn参数'], JSON_UNESCAPED_UNICODE),$this->id);
        }
        if(!isset($shipping_type)){
            $this->update_logs(json_encode(['code'=>-2,'message'=>'缺少shipping_type参数'], JSON_UNESCAPED_UNICODE),$this->id);
        }
        if(empty($logistics_arr) && $shipping_type == 1){
            $this->update_logs(json_encode(['code'=>-2,'message'=>'缺少logistics_arr参数'], JSON_UNESCAPED_UNICODE),$this->id);
        }
        $order = new VslOrderModel();
        $order_id = $order->getInfo(['order_no'=>$order_bn],'order_id')['order_id'];
        $order_info = $order_service->getOrderDetail($order_id);
        if(empty($order_info)){
            $this->update_logs(json_encode(['code'=>-1,'message'=>'订单不存在'], JSON_UNESCAPED_UNICODE),$this->id);
        }
        if($order_info['order_status'] !=1){
            $this->update_logs(json_encode(['code'=>-1,'message'=>'操作失败，订单状态已改变'], JSON_UNESCAPED_UNICODE),$this->id);
        }
        $goods = new VslGoodsModel();
        $order_good = new VslOrderGoodsModel();
        if(empty(json_decode($logistics_arr,true))){
            $logistics_arr = str_replace('&quot;', '"', $logistics_arr);
        }
        $logistics_arr = json_decode($logistics_arr,true);
        if(count($logistics_arr) == 1){ //单物流
            $is_more = 0;
            $order_goods_id = $order_good->getQuery(['order_id'=>$order_id],'order_goods_id,goods_id','');
            foreach ($order_goods_id as $key => $value) {
                $order_goods_id_arr[] = $value['order_goods_id'];
            }
            $order_goods_id_array = implode(",",$order_goods_id_arr);
            $res = $order_service->orderDelivery($order_id, $order_goods_id_array, $logistics_arr[0]['express_name'], $shipping_type, $logistics_arr[0]['express_company_id'], $logistics_arr[0]['express_no'],$is_more);
            $this->addUserLogByParam("订单发货", $res);
            //发货后将待发货状态改成待收获 
        }else if(count($logistics_arr) > 1){//多物流情况下
            $is_more = 1;
            $order_goods_data = $order_good->getQuery(['order_id'=>$order_id],'order_goods_id','');
            $order_goods_data = objToArr($order_goods_data);
            if(count($order_goods_data) == 1){
                $order_goods_id_array = $order_goods_data[0]['order_goods_id'];
            }else if(count($order_goods_data)  > 1){
                foreach ($order_goods_data as $key => $value) {
                    $array[] = $value['order_goods_id'];
                }
                $order_goods_id_array = implode(",",$array);
            }
            foreach ($logistics_arr as $key => $value) {   
                $res[] = $order_service->orderDelivery($order_id, $order_goods_id_array, $value['express_name'], $shipping_type, $value['express_company_id'], $value['express_no'],$is_more);
            }
            $this->addUserLogByParam("订单发货", $res);
        }else if(empty($shipping_type) || $shipping_type == 0){
            $order_goods_data = $order_good->getQuery(['order_id'=>$order_id],'order_goods_id','');
            $order_goods_data = objToArr($order_goods_data);
            if(count($order_goods_data) == 1){
                $order_goods_id_array = $order_goods_data[0]['order_goods_id'];
            }else if(count($order_goods_data)  > 1){
                foreach ($order_goods_data as $key => $value) {
                    $array[] = $value['order_goods_id'];
                }
                $order_goods_id_array = implode(",",$array);
            }
            $res = $order_service->orderGoodsDelivery($order_id, $order_goods_id_array);
            $this->addUserLogByParam("订单发货", $res);
        }else{
            $this->update_logs(json_encode(['code'=>-1,'message'=>'操作失败，物流信息为空'], JSON_UNESCAPED_UNICODE),$this->id);
        }
        if (!empty($memo)) {
            $data['order_id'] = $order_id;
            $data['memo'] = $memo;
            $data['uid'] = $this->uid;
            $data['create_time'] = time();
            $order_service->addOrderSellerMemoNew($data);
        }
        if($res){
            return json(['code'=>1,'message'=>'操作成功'],JSON_UNESCAPED_UNICODE);
        }
    }

    /*
     * 修改物流信息
     */
    public function updateOrderDelivery()
    {
        $order_service = new OrderService();
        $order_bn = request()->post('order_bn');
        $logistics_arr = request()->post('logistics_arr');
        $shipping_type = request()->post('shipping_type');
        $memo = request()->post('seller_memo');
        if(empty($order_bn)){
            $this->update_logs(json_encode(['code'=>-2,'message'=>'缺少order_bn参数'], JSON_UNESCAPED_UNICODE),$this->id);
        }
        if(empty($logistics_arr)){
            $this->update_logs(json_encode(['code'=>-2,'message'=>'缺少logistics_arr参数'], JSON_UNESCAPED_UNICODE),$this->id);
        }
        if(empty(json_decode($logistics_arr,true))){
            $logistics_arr = str_replace('&quot;', '"', $logistics_arr);
        }
        $order = new VslOrderModel();
        $order_id = $order->getInfo(['order_no'=>$order_bn],'order_id')['order_id'];
        $order_good = new VslOrderGoodsModel();
        $logistics_arr = json_decode($logistics_arr,true);
        $order_goods_express = new VslOrderGoodsExpressModel();
        $order_goods_express_data = $order_goods_express->getQuery(['order_id'=>$order_id],'id','');
        $order_goods_express_data = objToArr($order_goods_express_data);
        //如果更新的物流数组和原有的长度一样，则是更新，否则是删除，增加
        if(count($logistics_arr) == count($order_goods_express_data)){
            foreach ($order_goods_express_data as $key => $value) {
                $result = $order_service->updateDelivery($value['id'], $logistics_arr[$key]?$logistics_arr[$key]:[]);
            }
        }else{//删除掉
            foreach ($order_goods_express_data as $key => $value) {
                $result = $order_service->delDelivery(['id'=>$value['id']]);
            }
            //新增
            if(count($logistics_arr) == 1){ //单商品单物流
                $is_more = 0;
                $order_goods_id = $order_good->getInfo(['order_id'=>$order_id],'order_goods_id,goods_id')['order_goods_id'];
                $res = $order_service->orderDelivery($order_id, $order_goods_id, $logistics_arr[0]['express_name'], $shipping_type, $logistics_arr[0]['express_company_id'], $logistics_arr[0]['express_no'],$is_more);
                $this->addUserLogByParam("订单发货", $res);
                //发货后将待发货状态改成待收获 
            }else if(count($logistics_arr) > 1){//多物流情况下
                $is_more = 1;
                $order_goods_data = $order_good->getQuery(['order_id'=>$order_id],'order_goods_id','');
                $order_goods_data = objToArr($order_goods_data);
                if(count($order_goods_data) == 1){
                    $order_goods_id_array = $order_goods_data[0]['order_goods_id'];
                }else if(count($order_goods_data)  > 1){
                    foreach ($order_goods_data as $key => $value) {
                        $array[] = $value['order_goods_id'];
                    }
                    $order_goods_id_array = implode(",",$array);
                }
                foreach ($logistics_arr as $key => $value) {   
                    $res[] = $order_service->orderDelivery($order_id, $order_goods_id_array, $value['express_name'], $shipping_type, $value['express_company_id'], $value['express_no'],$is_more);
                }
                $this->addUserLogByParam("订单发货", $res);
            }else if(empty($shipping_type) || $shipping_type == 0){
                $order_goods_data = $order_good->getQuery(['order_id'=>$order_id],'order_goods_id','');
                $order_goods_data = objToArr($order_goods_data);
                if(count($order_goods_data) == 1){
                    $order_goods_id_array = $order_goods_data[0]['order_goods_id'];
                }else if(count($order_goods_data)  > 1){
                    foreach ($order_goods_data as $key => $value) {
                        $array[] = $value['order_goods_id'];
                    }
                    $order_goods_id_array = implode(",",$array);
                }
                $res = $order_service->orderGoodsDelivery($order_id, $order_goods_id_array);
                $this->addUserLogByParam("订单发货", $res);
            }else{
                $this->update_logs(json_encode(['code'=>-1,'message'=>'操作失败，物流信息为空'], JSON_UNESCAPED_UNICODE),$this->id);
            }
        }
        
        if(!empty($memo)) {
            $data['order_id'] = $order_id;
            $data['memo'] = $memo;
            $data['uid'] = $this->uid;
            $data['create_time'] = time();
            $order_service->addOrderSellerMemoNew($data);
        }
        $this->addUserLogByParam("更新订单", $order_id);
        if(is_array(AjaxReturn($result))){
            return json_encode(AjaxReturn($result),JSON_UNESCAPED_UNICODE);
        }else{
            return AjaxReturn($result);
        }
        
    }

    /**
     * [getOrderGoodsIdArray 获取]
     * @param  [type] $goods_code_array [description]
     * @return [type]             [description]
     */
    private function getOrderGoodsIdArray($goods_code_array,$order_id)
    {
        $goods = new VslGoodsModel();
        $order_good = new VslOrderGoodsModel();
        if(is_numeric($goods_code_array)){
            $goods_id_array = $goods->getInfo(['code'=>$goods_code_array],'goods_id')['goods_id'];
        }else{
            $goods_code_array = explode(",",$goods_code_array);
            foreach ($goods_code_array as $key => $value) {
                $array[] = $goods->getInfo(['code'=>$value],'goods_id')['goods_id'];
            }
            $goods_id_array = implode(",",$array);
        }
        if(is_numeric($goods_id_array)){
            $order_goods_id_array = $order_good->getInfo(['goods_id'=>$goods_id_array,'order_id'=>$order_id],'order_goods_id')['order_goods_id'];
        }else{
            $goods_id_array = explode(",",$goods_id_array);
            foreach ($goods_id_array as $key => $value) {
                $arr[] = $order_good->getInfo(['goods_id'=>$value,'order_id'=>$order_id],'order_goods_id')['order_goods_id'];
            }
            $order_goods_id_array = implode(",",$arr);
        }
        return $order_goods_id_array;
    }
   
}
